var  companyareaids =   $("#companyareaids").val();
var companyareaidsArray = companyareaids.split(",");
layui.use('form',function () {
    var form = layui.form;
    var $form = $('form');

    loadProvince();//加载区域数据

    form.on('select(province)', function(data) {
        var  pid = data.value;
        if (pid.length > 0){
            loadCity(pid);
        }
    })
    form.on('select(city)', function(data) {
        var  pid = data.value;
        if (pid.length > 0){
            loadArea(pid);
        }
    })
    form.on('select(area)', function(data) {
        var  pid = data.value;
        if (pid.length > 0){
            loadStreet(pid);
        }
    })
    form.on('select(street)', function(data) {
        var  pid = data.value;

    })



//加载省数据
function loadProvince() {
    var formDate = {
        areaPId:1
    }
    $.ajax({
        type: "POST",
        url: base+"/WeChatUser/wechatCompany/admin/getProvince",
        data: formDate,
        success: function (data) {
            //console.log(data)
            var clos = data.data;
            var proHtml='';
            for (const p in clos) {
                if (companyareaidsArray[0] == clos[p].areaId){
                    proHtml+= '<option selected value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                    loadCity(companyareaidsArray[0]);
                }else{
                    proHtml+= '<option value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                }
            }
            $("#province").append(proHtml);
            form.render();//菜单渲染 把内容加载进去
        }
    });
}

//加载市数据
function loadCity(pid) {
    var formDate = {
        areaPId:pid
    }
    $.ajax({
        type: "POST",
        url: base+"/WeChatUser/wechatCompany/admin/getProvince",
        data: formDate,
        success: function (data) {
            $form.find('select[name=area]').parent().hide();
            $form.find('select[name=street]').parent().hide();
            var clos = data.data;
            var cityHtml='<option value="">请选择市</option>';
            for (const p in clos) {
                if (companyareaidsArray[1] == clos[p].areaId){
                    cityHtml+= '<option selected value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                    loadArea(companyareaidsArray[1]);
                }else {
                    cityHtml+= '<option value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                }
            }
            $form.find('select[name=city]').html(cityHtml).parent().show();
            form.render();//菜单渲染 把内容加载进去
        }
    });
}

//加载区县数据
function loadArea(pid) {
    var formDate = {
        areaPId:pid
    }
    $.ajax({
        type: "POST",
        url: base+"/WeChatUser/wechatCompany/admin/getProvince",
        data: formDate,
        success: function (data) {
            $form.find('select[name=street]').parent().hide();
            var clos = data.data;
            var cityHtml='<option value="">请选择县/区</option>';
            for (const p in clos) {
                if (companyareaidsArray[2] == clos[p].areaId){
                    cityHtml+= '<option selected value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                    loadStreet(companyareaidsArray[2]);
                }else{
                    cityHtml+= '<option value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                }
            }
            $form.find('select[name=area]').html(cityHtml).parent().show();
            form.render();//菜单渲染 把内容加载进去
        }
    });
}

//加载街道数据
function loadStreet(pid) {
    var formDate = {
        areaPId:pid
    }
    $.ajax({
        type: "POST",
        url: base+"/WeChatUser/wechatCompany/admin/getProvince",
        data: formDate,
        success: function (data) {
            var clos = data.data;
            var cityHtml='<option value="">请选择街道</option>';
            for (const p in clos) {
                if (companyareaidsArray[3] == clos[p].areaId){
                    cityHtml+= '<option selected value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                }else {
                    cityHtml+= '<option value="'+clos[p].areaId+'">'+clos[p].areaName+'</option>';
                }
            }
            $form.find('select[name=street]').html(cityHtml).parent().show();
            form.render();//菜单渲染 把内容加载进去
        }
    });
}

})